package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.UpdateInterceptor;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.RedisUtils;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.enums.*;
import com.meida.module.bus.provider.service.BusOrderAnswerService;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.msg.client.entity.MsgReceive;
import com.meida.msg.provider.service.MsgReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 订单答案审核
 */
@Component
public class BusOrderAnswerCheckHandler implements UpdateInterceptor<BusOrderAnswer> {

    @Autowired
    private BusOrderAnswerService busOrderAnswerService;

    @Autowired
    private BusOrderService busOrderService;

    @Autowired
    private MsgReceiveService msgReceiveService;

    @Autowired
    private RedisUtils redisUtils;


    @Override
    public void prepare(CriteriaUpdate cu, EntityMap params, BusOrderAnswer busOrderAnswer) {
        Date checkTime = DateUtils.getNowDateTime();
        busOrderAnswer.setCheckTime(checkTime);
        Integer answerState = busOrderAnswer.getAnswerState();
        if (answerState.equals(AnswerStateEnum.checkPass.getValue())) {
            EntityMap settleConfig = redisUtils.getConfigMap("SETTLE_CONFIG");
            if (FlymeUtils.isNotEmpty(settleConfig)) {
                //默认取未接受答案的参数配置
                Integer noAcceptAnswerDay = settleConfig.getInt("noAcceptAnswerDay");
                //订单答案截止日期
                Date settleExpireTime = DateUtils.plusDateDays(checkTime, noAcceptAnswerDay);
                busOrderAnswer.setSettleExpireTime(settleExpireTime);
            }
        }
    }

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, BusOrderAnswer busOrderAnswer) {
        Long answerId = busOrderAnswer.getAnswerId();
        BusOrderAnswer orderAnswer = busOrderAnswerService.getById(answerId);
        Integer answerState = busOrderAnswer.getAnswerState();
        Long orderId = orderAnswer.getOrderId();
        BusOrder busOrder = busOrderService.getById(orderId);
        Integer orderType = busOrder.getOrderType();
        //todo 审核通过后
        if (answerState.equals(AnswerStateEnum.checkPass.getValue())) {
            //修改订单状态
            updateOrderState(orderId, orderType);
            //订单节点通知-->用户
            Long userId = busOrder.getUserId();
            sendUserMessage(userId, orderType);
        }
        //订单节点通知-->学霸
        Long answerUserId = busOrder.getAnswerUserId();
        sendAnswerUserMessage(answerUserId, orderType, answerState);
    }

    private void updateOrderState(Long orderId, Integer orderType) {
        //2.修改订单状态为已解答
        if (OrderTypeEnum.TakePhotosAskQuestions.getValue().equals(orderType)) {
            busOrderService.updateOrderState(orderId, OrderStatePEnum.OrderState4.getValue());
        }
        if (OrderTypeEnum.BookingTutoring.getValue().equals(orderType)) {
            busOrderService.updateOrderState(orderId, OrderStateYEnum.OrderState4.getValue());
        }
        if (OrderTypeEnum.HomeWork.getValue().equals(orderType)) {
            busOrderService.updateOrderState(orderId, OrderStateZEnum.OrderState4.getValue());
        }
        /*//3.n天打款到学霸余额(redis 过期通知)
            String redisKey = RedisKeyEnum.OrderAnswerId.getRedisKey() + answerId;
            System.out.println("redisKey-----------" + redisKey);
            EntityMap settleConfig = redisUtils.getConfigMap("SETTLE_CONFIG");
            if (FlymeUtils.isNotEmpty(settleConfig)) {
                //默认取未接受答案的参数配置
                Integer noAcceptAnswerDay = settleConfig.getInt("noAcceptAnswerDay");
                redisUtils.set(redisKey, answerId, 3600 * 24 * noAcceptAnswerDay);
         }*/
    }


    private void sendUserMessage(Long userId, Integer orderType) {
        if (OrderTypeEnum.TakePhotosAskQuestions.getValue().equals(orderType)) {
            String msgTitle = "拍照提问已解答通知";
            String msgContent = "恭喜您！您的拍照提问学霸已解答，请及时查看答案！感谢您使用留同学APP";
            sendAndPushMessage(userId, msgTitle, msgContent);
        }
        if (OrderTypeEnum.HomeWork.getValue().equals(orderType)) {
            String msgTitle = "作业无忧已解答";
            String msgContent = "恭喜您！您的作业无忧学霸已解答，请及时查看答案！感谢您使用留同学APP";
            sendAndPushMessage(userId, msgTitle, msgContent);
        }
    }

    private void sendAnswerUserMessage(Long userId, Integer orderType, Integer answerState) {
        if (OrderTypeEnum.TakePhotosAskQuestions.getValue().equals(orderType)) {
            if (answerState.equals(AnswerStateEnum.checkPass)) {
                String msgTitle = "拍照提问后台审核成功通知";
                String msgContent = "恭喜您！上传的拍照提问答案审核成功！感谢您使用留同学APP";
                sendAndPushMessage(userId, msgTitle, msgContent);
            }
            if (answerState.equals(AnswerStateEnum.checkFail)) {
                String msgTitle = "拍照提问后台审核失败通知";
                String msgContent = "很抱歉！上传的拍照提问答案审核失败！请及时修改的答案再提交审核，感谢您使用留同学APP";
                sendAndPushMessage(userId, msgTitle, msgContent);
            }

        }
        if (OrderTypeEnum.HomeWork.getValue().equals(orderType)) {
            if (answerState.equals(AnswerStateEnum.checkPass)) {
                String msgTitle = "作业无忧后台审核成功通知";
                String msgContent = "恭喜您！上传的作业无忧答案审核成功！感谢您使用留同学APP";
                sendAndPushMessage(userId, msgTitle, msgContent);
            }
            if (answerState.equals(AnswerStateEnum.checkFail)) {
                String msgTitle = "作业无忧后台审核失败通知";
                String msgContent = "很抱歉！上传的作业无忧答案审核失败！请及时修改的答案再提交审核，感谢您使用留同学APP";
                sendAndPushMessage(userId, msgTitle, msgContent);
            }

        }
    }

    private void sendAndPushMessage(Long userId, String msgTitle, String msgContent) {
        MsgReceive msgReceive = msgReceiveService.sendMsgReceiveNotice(BusOrder.class.getSimpleName(), userId, msgTitle, msgContent);
        busOrderService.orderPushMessage(msgReceive, msgTitle, msgContent);
    }
}
